# titlist.py - Render text and an image in full screen
#    Copyright (C) 2009  Alex Schoof
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Depends on pygame
import sys, pygame, threading, os
from pygame import *

class Titlist(threading.Thread):

    def __init__(self, displayNumber=os.environ["DISPLAY"]):
        """ Set up pygame modules """

        pygame.init()
	os.environ["DISPLAY"]=displayNumber
        self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        threading.Thread.__init__(self)


    def run(self):
        """ Threaded method to check for escape press """

        while True:
            for event in pygame.event.get():
                if pygame.key.get_pressed()[pygame.K_ESCAPE]:
                    sys.exit()

    def update(self, textArray, bgimage='default.jpg', fontColor=(0,0,0), spacerAMT=1.75, fontSize=56):
        """ Update the text and/or background in the Titlist. 

        textArray - an array or tuple, each element is a line of text to be displayed.
        bgImage - background for the titlist. A relative or absolute path may be used.
        fontColor - a tuple containing the font color in rgb.
        spacerAMT - an amount to scale the spacing between lines of text.
        fontSize - size of the font.
"""

        self.fontLines = textArray

        # Load background image
        self.background = pygame.image.load(bgimage).convert()
        self.backRect = self.background.get_rect()
        self.screen.blit(self.background, self.backRect)
        
        # Set up font object
        self.font = pygame.font.Font(None, fontSize)

        # build lists of actual text objects and their associated bounding boxes
        self.text = [self.font.render(line, True, fontColor) for line in self.fontLines]               
        self.textRect = [line.get_rect() for line in self.text]

        # Calculate the amount to offset the text from the center
        self.textSpacer = spacerAMT * self.textRect[0].height * (len(self.textRect)/2)
        
        for rect in self.textRect: 
            rect.centerx = self.screen.get_rect().centerx
            # Start the text above the center, then move it lower based on which line it is
            rect.centery = self.screen.get_rect().centery - self.textSpacer + (self.textRect.index(rect) * spacerAMT * rect.height)
        
        # Move the text onto the screen
        for i in range(len(self.fontLines)):
            self.screen.blit(self.text[i], self.textRect[i])
        
        pygame.display.update()


# Test Code:
if __name__ == "__main__":
    import time
    myTitle = Titlist()
    myTitle.start()
    myTitle.update(("First Line", "And another line!", "and one more!"))
    time.sleep(3) # you wont be able to escape during the wait... 
    myTitle.update(("Some more text", "and then more"))
